package com.example.ktapplication.utils;

import android.text.TextUtils;
import android.util.Log;


/**
 * 日志工具类
 */
public class LogHelper {
    private static String className;//类名
    private static String methodName;//方法名
    private static int lineNumber;//行数
    public static String LOG_TAG = "LogHelper";

    private LogHelper() {
    }

    public static boolean isDebuggable() {
        return true;
    }

    private static String formatLog(String log) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(methodName);
        buffer.append("(").append(className).append(":").append(lineNumber).append(")");
        buffer.append(log);
        return buffer.toString();
    }

    private static void getMethodNames(StackTraceElement[] sElements) {
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }


    public static void e(String message) {
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.e(TextUtils.isEmpty(LOG_TAG) ? className : LOG_TAG, formatLog(message));
    }

    public static void i(String message) {
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.i(className +"类>>"+methodName+ "方法>>" + LOG_TAG, formatLog(message));
    }

    public static void d(String message) {
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.d(TextUtils.isEmpty(LOG_TAG) ? className : LOG_TAG, formatLog(message));
    }

    public static void v(String message) {
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.v(TextUtils.isEmpty(LOG_TAG) ? className : LOG_TAG, formatLog(message));
    }

    public static void w(String message) {
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.w(TextUtils.isEmpty(LOG_TAG) ? className : LOG_TAG, formatLog(message));
    }

    public static void wtf(String message) {
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.wtf(TextUtils.isEmpty(LOG_TAG) ? className : LOG_TAG, formatLog(message));
    }
}